home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / tuner / sendto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-10  |  4.1 KB  |  136 lines

  1. /*    sendto.c
  2. #
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.     g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. %    Send images with options to a extended X server on a given host
  33. %
  34. % AUTHOR:    Jin Guojun - LBL    1/1/93
  35. */
  36. char    Usage[] =
  37. "\nusage: %s host [+udp] [+port #] [+rtp] [-d #] [-w #] [[| <] input] [-... options]",
  38.     *Progname, *s_name="s_master";
  39.  
  40. #include <sys/file.h>
  41. #include "net_need.h"
  42. #include "imagedef.h"
  43.  
  44. #ifndef    BufSize
  45. #define BufSize 32768
  46. #endif
  47.  
  48. int    window=Max_WINDOW_SIZE, total, udp, rtp, Delay;    /* in micro sec. */
  49. static    char    buf[BufSize]=" connect";
  50. U_IMAGE    uimg;
  51. #define    isarg(s)    !strncmp(av[n], s, 2)
  52. #define    hdlen    MAGIC_HEADER_LEN + sizeof(rtpmsg_hd)
  53.  
  54. main(int ac, char* av[])
  55. {
  56. struct    timeval    ts;
  57. struct    sockaddr_in    server;
  58. char    *hostp=buf+hdlen;
  59. int    s=0, n=2, buf_size=BufSize;
  60. rtpmsg_hd*    msg_hd;
  61.  
  62. in_fp = stdin;
  63. Progname = *av;
  64.  
  65. if (ac < n--)
  66. serr:    prgmerr(n, Usage, *av);
  67.  
  68.     while (++n < ac)
  69.         if (*av[n] == '+')    {
  70.         if (av[n][1]=='p' && n+1<ac)
  71.             s_name = av[++n];
  72.         else if (isarg("+u"))    udp = True,
  73.         window = UDP_BUF_LIMIT,    buf_size = 28672;
  74.         else if (isarg("+r"))    rtp = True;
  75.         else if (isarg("+d") && n+1<ac)
  76.             Delay = atoi(av[++n]);
  77.         else if (isarg("+w") && n+1<ac)
  78.             window = atoi(av[++n]);
  79.         else    goto    serr;
  80.         } else if (*av[n] == '-')    {
  81.         strcat(hostp, av[n]);
  82.         strcat(hostp, " ");
  83.         if (n+1<ac && *av[n+1] != '-' && *av[n+1] != '+')
  84.             strcat(hostp, av[++n]),
  85.             strcat(hostp, " ");
  86.         strcpy(buf, "#");    /* arg is set    */
  87.         }    else    s = n;    /* file name position    */
  88.  
  89. if (s && !(in_fp=fopen(av[s], "rb")))
  90.     goto    serr;
  91. io_test(fileno(in_fp), goto    serr);
  92.  
  93.     n = strlen(hostp);    hostp = av[1];
  94.     s = build_socket(hostp, s_name, udp<IP_SOCK ? udp+1 : IP_SOCK,
  95.         True, &window, &server, 0);
  96.     if (!udp)
  97.         if (connect(s, &server, sizeof(server)) /* for write, but sendto */
  98.         < 0)    prgmerr(1, buf);
  99.         else message("%s: connected to %s, port=%d <window %d>\n",
  100.         getenv("HOSTNAME"), hostp, server.sin_port, window);
  101.     else    message("send UDP to %s\n", hostp);
  102.  
  103.     msg_hd = (rtpmsg_hd*) &buf[MAGIC_HEADER_LEN];
  104.     ((short*)buf)[1] = htons(n);    /* params len    */
  105.     n += hdlen + 1;    /* for a NULL ending */
  106.     if (buf_size > window)
  107.         buf_size = window;
  108.     window = buf_size;
  109.  
  110.     gettimeofday(&ts, 0);
  111.     msg_hd->tss_s = htonl(ts.tv_sec);
  112.     msg_hd->tss_us = htonl(ts.tv_usec);
  113.  
  114.     if (!strcmp(buf, "#"))
  115.         if ((total=!udp ? write(s, buf, n) :
  116.         sendto(s, buf, n, 0, &server, sizeof(server))) != n)
  117.         syserr("sendto header to");
  118.        else    message("send params {%s} to %s\n", buf+hdlen, hostp);
  119.     do {
  120.         if (rtp)    buf_size = read(fileno(in_fp), buf, window);
  121.         else    buf_size = fread(buf, 1, window, in_fp);
  122.         if ((n = !udp ? write(s, buf, buf_size) :
  123.             sendto(s, buf, buf_size, 0, &server, sizeof(server))) < 1)
  124.             syserr("sento");
  125.         total += n;
  126.         if (Delay)    {
  127.             ts.tv_sec = 0;    ts.tv_usec = Delay;
  128.             select(1, 0, 0, 0, &ts);
  129.         }
  130. #ifdef    _DEBUG_
  131.         message("\r%d Bytes send to %s", total, hostp);
  132. #endif
  133.     } while (n == window || (rtp && n > 0));
  134. message("\rTotal %d Bytes send to %s\n", total, hostp);
  135. }
  136.